#include<stdio.h>
const int N=200005,K=1000005;

#define ll long long
const ll INF=90000000000000007ll;

inline void M(ll &x,ll y){
	x=x>y?x:y;
}

int T,n,a[N];
ll dp[N],s[N],f[K],t;

int main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);

	int i;
	scanf("%d",&T);

	for(i=0;i<K;i++) f[i]=-INF;

	while(T--){
		scanf("%d",&n);
		t=a[n+1]=0;

		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
			s[i]=s[i-1]+(a[i]==a[i-1]?a[i]:0);
		}

		for(i=1;i<=n;i++){
			dp[i]=f[a[i+1]]+a[i+1]+s[i];
			M(dp[i],t+s[i]);
			M(f[a[i]],dp[i]-s[i+1]);
			M(t,dp[i]-s[i+1]);
		}

		for(i=1;i<=n;i++) f[a[i]]=-INF;
		
		printf("%lld\n",dp[n]);
	}

	return 0;
}